package com.hoshiicloud.saleorder.service.impl.customer;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hoshiicloud.common.aop.Logable;
import com.hoshiicloud.common.exception.CommonError;
import com.hoshiicloud.common.exception.ServiceException;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.saleorder.feign.IdGeneratorService;
import com.hoshiicloud.saleorder.mapper.customer.GoodsBrowseRecordMapper;
import com.hoshiicloud.saleorder.service.customer.IGoodsBrowseRecordService;
import com.hoshiicloud.user.entity.customer.GoodsBrowseRecord;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 商品浏览记录 服务实现类
 * </p>
 *
 * @author Mr.Wu
 * @since 2019-04-28
 */
@Service
public class GoodsBrowseRecordServiceImpl extends ServiceImpl<GoodsBrowseRecordMapper, GoodsBrowseRecord>
        implements IGoodsBrowseRecordService {

    @Autowired
    private GoodsBrowseRecordMapper goodsBrowseRecordMapper;

    @Autowired
    private IdGeneratorService idGeneratorService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = {"GoodsBrowseRecord"}, allEntries = true)
    @Logable(authName = "前台用户操作-商品浏览记录信息管理-新增商品浏览记录信息")
    public Response insert(GoodsBrowseRecord param) {
        String id = idGeneratorService.queryGeneratorId();
        if (StringUtils.isBlank(id)) {
            throw new ServiceException(CommonError.error("id服务失败"));
        }
        param.setId(Long.parseLong(id));
        param.setCreatedtime(LocalDateTime.now());
        int count = goodsBrowseRecordMapper.insert(param);
        if (count <= 0) {
            throw new ServiceException(CommonError.error("新增失败"));
        }
        return Response.success("新增成功");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = {"GoodsBrowseRecord"}, allEntries = true)
    @Logable(authName = "前台用户操作-商品浏览记录信息管理-修改商品浏览记录信息")
    public Response updateDataById(GoodsBrowseRecord param) {
        int count = goodsBrowseRecordMapper.updateById(param);
        if (count <= 0) {
            throw new ServiceException(CommonError.error("修改失败"));
        }
        return Response.success("修改成功");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = {"GoodsBrowseRecord"}, allEntries = true)
    @Logable(authName = "前台用户操作-商品浏览记录信息管理-删除商品浏览记录信息")
    public Response deleteDataById(String id) {
        int count = goodsBrowseRecordMapper.deleteById(id);
        if (count <= 0) {
            throw new ServiceException(CommonError.error("删除失败"));
        } else {
            return Response.success("删除成功");
        }
    }

    @Override
    @Cacheable(cacheNames = "GoodsBrowseRecord", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#id")
    @Logable(authName = "前台用户操作-商品浏览记录信息管理-查询商品浏览记录信息详情")
    public Response getDetail(String id) {
        // 组装商品信息返回
        GoodsBrowseRecord data = goodsBrowseRecordMapper.selectById(id);
        if (data == null) {
            return Response.failed("数据不存在");
        } else {
            return Response.data(data);
        }
    }

    @Override
    @Cacheable(cacheNames = "GoodsBrowseRecord", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+T(com.hoshiicloud.common.utils.DigestUtils).md5(#key)")
    @Logable(authName = "前台用户操作-商品浏览记录信息管理-查询商品浏览记录信息列表")
    public Response getList(QueryWrapper<GoodsBrowseRecord> queryWrapper, String key) {
        // 组装商品信息返回
        return Response.data(goodsBrowseRecordMapper.selectList(queryWrapper));
    }

    @Override
    @Cacheable(cacheNames = "GoodsBrowseRecord",  key = "#root.targetClass.simpleName+':'+#root.methodName+':'+T(com.hoshiicloud.common.utils.DigestUtils).md5(#key)")
    @Logable(authName = "前台用户操作-商品浏览记录信息管理-分页查询商品浏览记录信息")
    public Response getPage(IPage<GoodsBrowseRecord> page, QueryWrapper<GoodsBrowseRecord> queryWrapper, String key) {
        // 组装商品信息返回
        return Response.data(goodsBrowseRecordMapper.selectPage(page, queryWrapper));
    }

    @Override
    @Cacheable(cacheNames = "GoodsBrowseRecord",  key = "#root.targetClass.simpleName+':'+#root.methodName")
    @Logable(authName = "前台用户操作-商品浏览记录信息管理-分页查询商品浏览记录信息")
    public List<Map<String, Object>> getListGroupByBaseSpuId() {
        return goodsBrowseRecordMapper.getListGroupByBaseSpuId();
    }
}
